package com.interview.item.javabase.singleton;

/**
 * @Author:zxh
 * @Description: 设计模式：单例模式
 * 1.私有化的构造方法  2.私有静态全局变量   3.公有静态方法
 * @Date: 2020/3/24
 * @Modified By:
 */
public class singletonDemo {

    /**
     * @Author:zxh
     * @Description: 饿汉式
     * @Date: 2020/3/24
     */
    static class singletonOne {
        private singletonOne() {
        }


        private static singletonOne singleton = new singletonOne();

        public static singletonOne getSingleton() {
            return singleton;
        }

    }


    /**
     * @Author:zxh
     * @Description: 懒汉式
     * @Date: 2020/3/24
     */
    static class singleonTwo {
        private singleonTwo() {
        }


        private static singleonTwo singleonTwo = null;


        public singleonTwo getSingleonTwo() {
            if (singleonTwo == null) {
                singleonTwo = new singleonTwo();
            }
            return singleonTwo;
        }
    }

    /**
     * @Author:zxh
     * @Description: 线程安全
     * @Date: 2020/3/24
     */
    static class singletionT {
        private singletionT() {
        }

        private static singletionT single;

        public singletionT getSingle() {
            if (null == single) {
                synchronized (singletionT.class) {
                    if (null == single) {
                        single = new singletionT();
                    }
                }
            }
            return single;

        }
    }
}
